/*
* $Id: TableCelRow.cpp 152 2010-09-23 18:28:55Z volok-aleksej@yandex.ru $
*
* Copyright 2009 by Volokitin Alexey
* All rights reserved
*
*/
#include "Common/include.h"
#include "Xml/TableXml.h"
#include "TableCelRow.h"
#include "TableCel.h"

CTableCelRow::CTableCelRow(int nId, int nType)
: m_nId(nId), m_pBack(NULL), m_pNext(NULL), m_nType(nType){
	GetXmlCy();
}

CTableCelRow::CTableCelRow(const CTableCelRow& row){
	*this = row;
}
CTableCelRow::~CTableCelRow(){
	for(std::vector<CTableCel*>::iterator it = m_aCels.begin(); it != m_aCels.end(); it++)
		delete *it;
	SetXmlCy();
}

bool CTableCelRow::Paint(CDC* dc, CRect rect){
	if(m_posY > rect.bottom)
		return false;
	for(std::vector<CTableCel*>::iterator it = m_aCels.begin(); it != m_aCels.end(); it++){
		(*it)->SetYPos(m_posY);
		(*it)->Init();
		(*it)->Paint(dc);
	}
	return true;
}

void CTableCelRow::AddCel(tableHeader* pHeader, const std::tstring& strValue){
	CTableCel* tableCel = new CTableCel(strValue, pHeader);
	tableCel->SetCy(GetCY());
	m_aCels.push_back(tableCel);
}

void CTableCelRow::operator = (const CTableCelRow& row){
	m_aCels = row.m_aCels;
	m_posY = row.m_posY;
	m_pBack = row.m_pBack;
	m_pNext = row.m_pNext;
	if(m_pBack)
		m_pBack->m_pNext = this;
	if(m_pNext)
		m_pNext->m_pBack = this;
}

void CTableCelRow::SetPosY(int posY){
	m_posY = posY;
}
void CTableCelRow::SetCY(int cy){
	m_nCy = cy;
	for(std::vector<CTableCel*>::iterator it = m_aCels.begin(); it != m_aCels.end(); it++)
		(*it)->SetCy(cy);
}

int CTableCelRow::GetCY(){
	return m_nCy;
}
int CTableCelRow::GetId(){
	return m_nId;
}

void CTableCelRow::Select(bool bSel){
	for(std::vector<CTableCel*>::iterator it = m_aCels.begin(); it != m_aCels.end(); it++){
		if(bSel)
			(*it)->SetColor(RGB(200, 200, 200));
		else
			(*it)->SetColor(RGB(255, 255, 255));
	}
}

int CTableCelRow::GetYPos(){
	return m_posY;
}

std::tstring CTableCelRow::GetValueColumn(int nId){
	for(std::vector<CTableCel*>::iterator it = m_aCels.begin(); it != m_aCels.end(); it++){
		if(((tableHeader*)((*it)->m_pHeader))->m_nId == nId)
			return (*it)->m_strValue;
	}
	return _T("");
}

bool CTableCelRow::IsSetCursor(CPoint pos){
	return m_posY + GetCY() >= pos.y && m_posY + GetCY() - 3 < pos.y;
}

void CTableCelRow::GetXmlCy(){
	if(!(m_nCy = g_tableXml.GetCellRowCy(m_nId, m_nType)))
		m_nCy = (CItemTable::sizeFont + 5) * 2;
}

void CTableCelRow::SetXmlCy(){
	g_tableXml.SetCellRowCy(m_nId, m_nType, m_nCy);
}

TypeField::type CTableCelRow::GetTypeColumn(int nId){
	for(std::vector<CTableCel*>::iterator it = m_aCels.begin(); it != m_aCels.end(); it++){
		if(((tableHeader*)((*it)->m_pHeader))->m_nId == nId)
			return ((tableHeader*)(*it)->m_pHeader)->m_type;
	}
	return TypeField::STRING;
}
